#!/bin/sh
# vim: ft=sh ts=4 sw=4 et
# gen-maybe
# Ralph Becket <rafe@csse.unimelb.edu.au>
# Thu Apr  1 19:46:04 EST 2010
#
# Creates a new 'maybe' type with a value field (NULL indicates absence).

PROGNAME=`basename $0`

usage () {
cat << END_OF_USAGE
usage: $PROGNAME <type name> <item type> [option ...] [<.h file> ...]
where option is one of
    --malloc <malloc func>
    --free <free func>
Generates .h and .c files for a new 'maybe' type with
constructor <type name>_value(), where NULL indicates
absence of value.  The <.h file>s are #included in the
.h file header.
END_OF_USAGE
}

if test "$1" = "--help"
then
    usage
    exit 0
fi

if test "$#" -lt 2
then
    usage
    exit 1
fi

TYPE_NAME=$1
ITEM_TYPE=$2
shift 2
MALLOC=malloc
FREE=free
INCLUDES=""
while test "$#" -gt 0
do
    case "$1" in
    --malloc)
        MALLOC=$2
        shift 2
        ;;
    --free)
        FREE=$2
        shift 2
        ;;
    *)
        INCLUDES=$INCLUDES"#include \"$1\""
        shift
        ;;
    esac
done
INCLUDES=`echo $INCLUDES | sed 's/#/\n#/g'`

gen-du-type << END_OF_DU
@ type ${TYPE_NAME}
@ malloc_and_free ${MALLOC} ${FREE}
@ public_header
${INCLUDES}
@ constructor ${TYPE_NAME}_value
value : ${ITEM_TYPE}
@ end
END_OF_DU
